﻿// Author:					HungNM
// Created:					16-08-2012
// Last Modified:			16-08-2012
//

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eProject.Business;


namespace eProject.Web.UI
{
    public partial class LoginPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadParams();
            LoadSettings();
            PopulateLabels();
            PopulateControls();
        }

        private void PopulateControls()
        {
            if (IsPostBack) return;

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string username = txtUserName.Text.Trim().ToString();
            string password = txtPassword.Text.Trim().ToString();
            Users user = new Users(username);
            if (user.Id > 0)
            {
                string newPassword = EncryptHelper.Encrypt(password, user.ChallengeCode, true);

                if (newPassword.Equals(user.Password))
                {
                    UserHelper.Instance.AddUser(user.Id);
                    Response.Redirect(SessionClass.AuthRequestPage);
                }
                else
                {
                    ResultError.Text = "User or Password not found.";
                    Result.Visible = true;
                }
            }
            else
            {
                ResultError.Text = "User or Password not found.";
                Result.Visible = true;
            }
        }

        private void PopulateLabels()
        {
            Title = "Login Page - eProject";
        }

        private void LoadSettings()
        {
            Users user = SiteUser.getCurrentUser();
            if (user != null && user.Id > 0)
                Response.Redirect(SessionClass.AuthRequestPage);
        }

        private void LoadParams()
        {

        }

        #region OnInit

        override protected void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Load += new EventHandler(this.Page_Load);
            this.btnSubmit.Click += new EventHandler(btnSubmit_Click);
        }

        #endregion
    }
}